import sublime, sublime_plugin
import os
import subprocess
import zipfile
import glob
import threading
from datetime import datetime, date, time

# 打开配置文件
def openSettingFile(view, flag):

	window = view.window()

	root = os.path.join(sublime.packages_path(), "wlgame-plugin")

	filePath = None

	if flag == 0:
		filePath = os.path.join(root, "wlgame-plugin.sublime-settings")

	if filePath:
		window.open_file(filePath)
	else:
		sublime.message_dialog("配置文件不存在")
	

# 运行模拟器命令
class wlgame_run_mahjongclientCommand(sublime_plugin.TextCommand):

	# userIndex对应keymap中传递的参数
	def run(self, edit, userIndex=0):
		print("RunMahjongClientCommand")
		
		view = self.view
		# 获取大厅的路径
		settings = sublime.load_settings("wlgame-plugin.sublime-settings")
		gameHallPath = settings.get("game_hall_path")
		if gameHallPath == None or len(gameHallPath) == 0:
			sublime.message_dialog("game-hall-client 路径未配置")
			openSettingFile(self.view, 0)
			return 

		# 暂时只处理windows下的情况
		if sublime.platform() == "windows":
			# 切换工作目录至win32下
			path = "%s/simulator/win32/" % gameHallPath
			os.chdir(path)
			# 执行命令
			command = "MahjongClient.exe -resolution 640x360"
			if userIndex != None and userIndex > 0:
				command = command + (" -userPath %s/user%s" % (path, userIndex))
			subprocess.Popen(command)

# 删除模拟器数据
class wlgame_del_mahjongclientCommand(sublime_plugin.TextCommand):

	def run(self, edit, userIndex):
		print("DelMahjongClient")
		# 获取大厅的路径
		settings = sublime.load_settings("wlgame-plugin.sublime-settings")
		gameHallPath = settings.get("game_hall_path")
		if gameHallPath == None or len(gameHallPath) == 0:
			return 

		# 暂时只处理windows下的情况
		if sublime.platform() != "windows":
			return 
			
		if userIndex == None or userIndex == 0:
			# 删除默认模拟顺数据 
			path = os.path.join(gameHallPath, "UserDefault.xml")
			if sublime.platform() == "windows":
				cmd = "del /a/f/q %s" % path
			else:
				cmd = "rm -f %s" % path

			os.system(cmd)
			return 

		if userIndex > 0:
			path = os.path.join(gameHallPath, ("simulator\\win32\\user%s" % userIndex))
			cmd = "ls"
			if sublime.platform() == "windows":
				cmd = "rd /s/q %s" % path
			else:
				cmd = "rm -rf %s" % path
			os.system(cmd)


class wlgame_build_win32Command(sublime_plugin.TextCommand):

	# 压缩文件
	def zipFiles(self, zipFileName, path, view):
		f = zipfile.ZipFile(zipFileName, 'w', zipfile.ZIP_DEFLATED)
		for dirpath, dirnames, filenames in os.walk(path):
			fpath = dirpath.replace(path,'') # 这一句很重要，不replace的话，就从根目录开始复制
			fpath = fpath and fpath + os.sep or ''# 实现当前文件夹以及包含的所有文件的压缩
			for filename in filenames:
				if os.path.isdir(dirpath):
					if dirpath.find("log") >= 0 or dirpath.find("user") >= 0:
						continue

					if filename == "UserDefault.xml" or (filename.find("get_") == 0 and filename.find(".lua") < 0):
						print ("filename: ", filename)
						continue

				f.write(os.path.join(dirpath, filename), fpath + filename)
		f.close()
		view.show_popup("<h3></h3><h3>　　(o゜▽゜)o☆[BINGO!]　　</h3><h3>　　打包win32完成<h3>")
		print("打包win32完成")

		def openFolder():
			cmd = "explorer ."
			subprocess.Popen(cmd)

		sublime.set_timeout(openFolder, 1.0 * 1000)

	def run(self, edit):

		view = self.view

		settings = sublime.load_settings("wlgame-plugin.sublime-settings")
		gameHallPath = settings.get("game_hall_path")

		if gameHallPath == None or len(gameHallPath) == 0:
			sublime.message_dialog("game-hall-client 路径未配置")
			return
		
		simulatorPath = os.path.join(gameHallPath, "simulator")
		win32Path = os.path.join(gameHallPath, "simulator", "win32")

		os.chdir(simulatorPath)

		dt = datetime.now()
		# 压缩包的名字，按日期区分
		zipFileName = 'win32_%s%s%s.zip' % (dt.year, dt.month, dt.day) 
		print("压缩包：", zipFileName)

		tips = "<h3></h3><h3>　　　　　　(´･_･`)</h3><h3>开始构建win32模拟器...</h3><h3></h3>"
		view.show_popup(tips)
		t = threading.Thread(target=self.zipFiles, args=(zipFileName, win32Path, view))
		t.start()
